/*
 * @Author: szx
 * @Date: 2022-03-19 13:48:44
 * @LastEditTime: 2022-03-19 14:10:21
 * @Description:
 * @FilePath: \leetcode\1-99\56\56_2.js
 */
/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */
var merge = function (intervals) {
    intervals.sort((a, b) => a[0] - b[0]);
    const res = [];
    let left = intervals[0][0];
    let right = intervals[0][1];
    for (let i of intervals) {
        if (i[0] > right) {
            res.push([left, right]);
            left = i[0];
            right = i[1];
        } else if (i[1] > right) right = i[1];
    }
    res.push([left, right]);
    return res;
};
console.log(
    merge([
        [1, 4],
        [0, 4]
    ])
);
console.log(
    merge([
        [1, 4],
        [2, 3]
    ])
);
console.log(
    merge([
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9],
        [1, 10]
    ])
);
console.log(
    merge([
        [1, 3],
        [2, 6],
        [8, 10],
        [15, 18]
    ])
);
